package com.xinzhitong.www.service.impl;

import com.xinzhitong.www.exception.BusyException;
import com.xinzhitong.www.exception.trainorganinfo.MultiBoundTrainOrganInfoException;
import com.xinzhitong.www.mapper.TrainMapper;
import com.xinzhitong.www.mapper.TrainOrganMapper;
import com.xinzhitong.www.pojo.TrainOrgan;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.TrainOrganUserInfoService;
import com.xinzhitong.www.utils.FileHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构用户信息服务实现类
 *
 * @author 徐鑫，黄欢欢
 */
@Service
@Transactional
public class TrainOrganUserInfoServiceImpl implements TrainOrganUserInfoService {

    private static final String WHOLEPATH = "/static/trainOrgan";

    private static final String PICTURESPATH = "/pictures";

    @Resource
    TrainOrganMapper trainOrganMapper;

    @Resource
    TrainMapper trainMapper;

    @Override
    public void editTrainOrganUserInfo(TrainOrgan trainOrgan, MultipartFile logo, MultipartFile[] pictures) {
        //TODO...
    }

    @Override
    public void createTrainOrganUserInfo(TrainOrgan trainOrgan, MultipartFile logo, MultipartFile[] pictures) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (trainOrganMapper.selectTrainOrganByUsername(user.getUsername()) != null)
            throw new MultiBoundTrainOrganInfoException();

        trainOrgan.setUsername(user.getUsername());
        if (trainOrganMapper.insertTrainOrgan(trainOrgan) == 0)
            throw new BusyException();
        trainOrgan = trainOrganMapper.selectTrainOrganByUsername(user.getUsername());
        if (trainOrgan == null)
            throw new BusyException();

        try {
            //生成logo存取路径
            String logoPath = WHOLEPATH + "/" + trainOrgan.getId() + "/logo.jpg";
            //保存图片
            FileHelper.saveTo(logoPath, logo);
            //更改logo属性
            if (trainOrganMapper.updateTrainOrganOfOneField(
                    "logo",
                    logoPath,
                    "username='" + trainOrgan.getUsername() + "'"
            ) == 0)
                throw new BusyException();

            //批量保存图片
            if (pictures.length != 0)
                trainOrganMapper.deleteTrainOrganHasPicture(trainOrgan.getId());
            for (int i = 0; i < pictures.length; i++) {
                //生成图片保存路径
                String picturePath = WHOLEPATH + "/" + trainOrgan.getId() + PICTURESPATH + "/" + i + ".jpg";
                //保存图片
                FileHelper.saveTo(picturePath, pictures[i]);
                //添加图片字段
                if (trainOrganMapper.insertTrainOrganHasPicture(trainOrgan.getId(), picturePath) == 0)
                    throw new BusyException();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusyException();
        }
    }

    @Override
    public void createTrainOrganUserInfoAcceleratePictures(TrainOrgan trainOrgan, Integer logoNum, Integer picturesNum) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (trainOrganMapper.selectTrainOrganByUsername(user.getUsername()) != null)
            throw new MultiBoundTrainOrganInfoException();

        trainOrgan.setUsername(user.getUsername());
        if (trainOrganMapper.insertTrainOrgan(trainOrgan) == 0)
            throw new BusyException();
        trainOrgan = trainOrganMapper.selectTrainOrganByUsername(user.getUsername());
        if (trainOrgan == null)
            throw new BusyException();


        List<Map<String, Object>> x = new ArrayList<>();

        //生成logo存取路径
        String logoPath = WHOLEPATH + "/" + trainOrgan.getId() + "/logo.jpg";
        //更改logo属性
        if (trainOrganMapper.updateTrainOrganOfOneField(
                "logo",
                logoPath,
                "username='" + trainOrgan.getUsername() + "'"
        ) == 0)
            throw new BusyException();

        //批量保存图片
        if (picturesNum != 0)
            trainOrganMapper.deleteTrainOrganHasPicture(trainOrgan.getId());
        for (int i = 0; i < picturesNum; i++) {
            //生成图片保存路径
            String picturePath = WHOLEPATH + "/" + trainOrgan.getId() + PICTURESPATH + "/" + i + ".jpg";
            //添加图片字段
            if (trainOrganMapper.insertTrainOrganHasPicture(trainOrgan.getId(), picturePath) == 0)
                throw new BusyException();
        }
    }


    @Override
    public Map<String, Integer> getView() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        TrainOrgan trainOrgan = trainOrganMapper.selectTrainOrganByUsername(user.getUsername());
        Map<String, Integer> res = new HashMap<>();
        res.put("trainClassTimeCount", trainMapper.countTrainClassTimeOfTrainOrgan(trainOrgan.getId()));
        res.put("peopleCount", trainMapper.countBuyTrainClassTimeOfTrainOrgan(trainOrgan.getId()));
        return res;
    }

    @Override
    public TrainOrgan getInfo() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        TrainOrgan info = trainOrganMapper.selectTrainOrganByUsername(user.getUsername());
        info.setLogo(FileHelper.fileURL + info.getLogo());
        return info;
    }


    /**
     *获取机构名和机构id服务实现类
     *
     *@return name,id
     */
    @Override
    public List<Map<String,Object>> getNameAndId(){
        Subject subject= SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Map<String,Object>> trainOrganNameAndId =trainOrganMapper.selectTrainOrganNameAndId();
        Map<String,Object> trainOrganNI = new HashMap<>();
        trainOrganNI.put("name",trainOrganMapper.selectTrainOrganNameAndId());
        trainOrganNI.put("id",trainOrganMapper.selectTrainOrganNameAndId());
        return trainOrganNameAndId;
    }

}
